package com.shouer.doc.ui.neworder.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.drawee.view.SimpleDraweeView;
import com.shouer.doc.R;
import com.shouer.doc.common.ShouerConst;
import com.shouer.doc.common.util.AppUtil;
import com.shouer.doc.common.util.DateFormatUtil;
import com.shouer.doc.common.util.RelativeDateFormatUtil;
import com.shouer.doc.ui.neworder.model.SimpleOrderModel;
import com.shouer.doc.ui.neworder.view.INewOrdView;
import com.shouer.doc.ui.ordermanager.adapter.OrderInOrderListAdapter;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by YYY-Work on 2017/4/20.
 */

public class NewOrderAdapter extends RecyclerView.Adapter<NewOrderVH> implements View.OnClickListener {

    private Context mContext;
    private List<SimpleOrderModel> mOrderModels;
    private LayoutInflater layoutInflater;
    private INewOrdView newOrdView;

    public NewOrderAdapter(Context context, List<SimpleOrderModel> orderModels, INewOrdView newOrdView) {
        this.mContext = context;
        this.mOrderModels = orderModels;
        layoutInflater = LayoutInflater.from(context);
        this.newOrdView = newOrdView;
    }

    @Override
    public NewOrderVH onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = layoutInflater.inflate(R.layout.fg_neworder_item_layout, null);
        return new NewOrderVH(itemView);
    }

    @Override
    public void onBindViewHolder(NewOrderVH holder, int position) {

        SimpleOrderModel simpleOrderModel = mOrderModels.get(position);
        //设置订单图标
        setOrderIco(holder.orderIcoView, simpleOrderModel);
        if (simpleOrderModel.getChannel() == 4||simpleOrderModel.getChannel() == 8) {
            //挂号送药
            holder.patNameView.setText(simpleOrderModel.getPatient().getPatName());
            holder.patAgeView.setText("");
            holder.patGenderView.setText("");
        } else {
            holder.patNameView.setText(simpleOrderModel.getPatient().getPatName());
            int age = 0;
            try {
                age = DateFormatUtil.getAgeByBirthday(DateFormatUtil.stringToDate(simpleOrderModel.getPatient().getPatBirthday().replace("T", "\b"), "yyyy-MM-dd"));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            age = age < 0 ? 0 : age;
            holder.patAgeView.setText(mContext.getResources().getString(R.string.patient_age, age));
            holder.patGenderView.setText(simpleOrderModel.getPatient().getPatSex() == 0 ? "女" : "男");
        }

        try {
            holder.timeView.setText(RelativeDateFormatUtil.format(DateFormatUtil.stringToDate(simpleOrderModel.getCreateTime().replace("T", "\b"), "yyyy-MM-dd\bHH:mm:ss")));
        } catch (Exception e) {
            Log.d(NewOrderAdapter.class.getSimpleName(), e.toString());
            holder.timeView.setText("");
        }

        if (simpleOrderModel.getOrderType() == ShouerConst.ORDER_TYPE_TUWEN) {
            holder.patDescribe.setText(simpleOrderModel.getPatDescribe());
            holder.orderPreLayout.setVisibility(View.GONE);
            holder.patDescribe.setVisibility(View.VISIBLE);
        } else if (simpleOrderModel.getOrderType() == ShouerConst.ORDER_TYPE_DIANHUA || simpleOrderModel.getOrderType() == ShouerConst.ORDER_TYPE_SHIPING) {

            try {
                Date createDate= DateFormatUtil.stringToDate(simpleOrderModel.getScheduleTime().replace("T","\b"),"yyyy-MM-dd\bHH:mm:ss");
                holder.orderTimeView.setText(DateFormatUtil.dateToString(createDate,"yyyy-MM-dd HH:mm"));
            } catch (ParseException e) {
                Log.e(NewOrderAdapter.class.getSimpleName(),e.toString());
                holder.orderTimeView.setText("-");
            }
            holder.orderDurationView.setText(mContext.getResources().getString(R.string.order_duration, simpleOrderModel.getOrderTime()));
            if (simpleOrderModel.getOrderType() == ShouerConst.ORDER_TYPE_DIANHUA) {
                holder.telView.setText(simpleOrderModel.getContactTel());
                holder.telLayout.setVisibility(View.VISIBLE);
            } else {
                holder.telLayout.setVisibility(View.GONE);
            }

            holder.patDescribe.setVisibility(View.GONE);
            holder.orderPreLayout.setVisibility(View.VISIBLE);
        } else {
            holder.patDescribe.setVisibility(View.GONE);
            holder.orderPreLayout.setVisibility(View.GONE);
        }


        if (TextUtils.isEmpty(simpleOrderModel.getDocId())) {
            holder.refuseBtn.setVisibility(View.GONE);
        } else {
            holder.refuseBtn.setVisibility(View.VISIBLE);
        }

        holder.itemView.setTag(R.id.list_position, position);
        holder.actionView.setTag(R.id.list_position, position);
        holder.refuseBtn.setTag(R.id.list_position, position);
        holder.itemView.setOnClickListener(this);
        holder.actionView.setOnClickListener(this);
        holder.refuseBtn.setOnClickListener(this);

    }


    private void setOrderIco(SimpleDraweeView orderIco, SimpleOrderModel orderModel) {
        String icoUriString = null;
        switch (orderModel.getChannel()) {
            case 1:
                icoUriString = "res://" + AppUtil.getPackageName(mContext) + "/" + R.drawable.ico_order_type_mfzx;
                break;
            case 2:
                icoUriString = "res://" + AppUtil.getPackageName(mContext) + "/" + R.drawable.ico_order_type_zjzx;
                break;
            case 3:
                icoUriString = "res://" + AppUtil.getPackageName(mContext) + "/" + R.drawable.ico_order_type_audio;
                break;
            case 4:
                icoUriString = "res://" + AppUtil.getPackageName(mContext) + "/" + R.drawable.ico_order_type_ghsy;
                break;
            case 6:
                icoUriString = "res://" + AppUtil.getPackageName(mContext) + "/" + R.drawable.ico_order_type_video;
                break;
            case 7:
                icoUriString = "res://" + AppUtil.getPackageName(mContext) + "/" + R.drawable.ico_order_type_quickask;
                break;
            case 8:
                icoUriString = "res://" + AppUtil.getPackageName(mContext) + "/" + R.drawable.ico_order_type_ghsy;
                break;
            case 9:
                icoUriString = "res://" + AppUtil.getPackageName(mContext) + "/" + R.drawable.ico_order_type_jkbg;
                break;
        }
        Uri uri = Uri.parse(icoUriString);
        orderIco.setImageURI(uri);

    }

    @Override
    public int getItemCount() {
        if (null == mOrderModels) {
            return 0;
        } else {
            return mOrderModels.size();
        }
    }

    public void refreshDocList(List<SimpleOrderModel> orderModels) {
        Log.i(NewOrderAdapter.class.getSimpleName(), "refreshDocList");
        this.mOrderModels = orderModels;
        notifyDataSetChanged();
    }

    public void attachToDocList(List<SimpleOrderModel> orderModels) {
        Log.i(NewOrderAdapter.class.getSimpleName(), "attachToDocList");
        if (null != orderModels) {
            if (null == mOrderModels) {
                mOrderModels = new ArrayList<>(0);
            }

            mOrderModels.addAll(orderModels);
            notifyDataSetChanged();
        }
    }

    public void removeOrder(SimpleOrderModel simpleOrderModel) {
        try {
            mOrderModels.remove(simpleOrderModel);
            notifyDataSetChanged();
        } catch (Exception e) {
            Log.e(NewOrderAdapter.class.getSimpleName(), "removeOrder Exception:" + e.toString());
        }
    }


    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.order_item:
                int position = (int) view.getTag(R.id.list_position);
                SimpleOrderModel simpleOrderModel = mOrderModels.get(position);
                newOrdView.newOrderItemClick(simpleOrderModel);
                break;
            case R.id.action_btn:
                int actionPosition = (int) view.getTag(R.id.list_position);
                SimpleOrderModel orderModel = mOrderModels.get(actionPosition);
                newOrdView.receiveOrder(orderModel);
                break;
            case R.id.refuse_btn:
                int refusePosition = (int) view.getTag(R.id.list_position);
                SimpleOrderModel refuseOrderModel = mOrderModels.get(refusePosition);
                newOrdView.refuseOrder(refuseOrderModel);
                break;
        }

    }
}
